package com.example.administrator.personalmail.fragment;
import android.support.v4.app.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.example.administrator.personalmail.R;
import com.example.administrator.personalmail.adapter.CategoryFirstAdapter;
import com.example.administrator.personalmail.adapter.CategorySecondAdapter;
import com.example.administrator.personalmail.adapter.HotAdapter;
import com.example.administrator.personalmail.api.Contants;
import com.example.administrator.personalmail.model.Banner;
import com.example.administrator.personalmail.model.Category;
import com.example.administrator.personalmail.model.Page;
import com.example.administrator.personalmail.model.Wares;
import com.example.administrator.personalmail.retrofitAPI.GetRequest_Interface;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2018/4/14 0014.
 */

public class FragmentThree extends Fragment{
    private RecyclerView rvFirst;
    private RecyclerView rvSecond;
    private View view;
    private CategoryFirstAdapter categoryFirstAdapter;
    private ArrayList<Banner> mBannersList;
    private SliderLayout sliderShow;
    private int curPage=1;
    private long mCategoryId=1;

    private Page<Wares> mPage;
    private ArrayList<Wares> mWares;
    private CategorySecondAdapter mCategorySecondAdapter;
    private MaterialRefreshLayout materialRefreshLayout;
    private int totalPage=0;
    private int REFRESH=1;
    private int LOADMORE=2;
    private int NORMAL=3;
    private int state=NORMAL;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_three,container,false);

        initBanners();
        initFirstCategory();
        initSecondCategory();
        return view;
    }

    private void initSecondCategory() {
        rvSecond = view.findViewById(R.id.rvCategory_second);
        rvSecond.setLayoutManager(new GridLayoutManager(getContext(),2));

        materialRefreshLayout = view.findViewById(R.id.refresh_category);
        materialRefreshLayout.setLoadMore(true);
        materialRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
                state = REFRESH;
                curPage=1;
                requestSecond();
            }

            @Override
            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                if (curPage<=totalPage){
                    state=LOADMORE;
                    curPage++;
                    requestSecond();
                }else {
                    materialRefreshLayout.finishRefreshLoadMore();
                }
            }
        });

        requestSecond();

    }

    private void requestSecond() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Contants.API.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        GetRequest_Interface service = retrofit.create(GetRequest_Interface.class);
        retrofit2.Call<Page<Wares>> call = service.categorySecondRequest(curPage,10,mCategoryId);

        call.enqueue(new Callback<Page<Wares>>() {
            @Override
            public void onResponse(Call<Page<Wares>> call, Response<Page<Wares>> response) {
                mPage = response.body();
                if (state == LOADMORE) {
                    mCategorySecondAdapter.addData(mCategorySecondAdapter.getSize(),mPage.getList());
                    if (curPage==totalPage){
                        mCategorySecondAdapter.setFOOTER(true);
                    }
                    materialRefreshLayout.finishRefreshLoadMore();
                }else if (state == REFRESH){
                    mWares = mPage.getList();
                    mCategorySecondAdapter = new CategorySecondAdapter(mWares);
                    mCategorySecondAdapter.setFOOTER(false);
                    rvSecond.setAdapter(mCategorySecondAdapter);
                    materialRefreshLayout.finishRefresh();
                }else {
                    mWares = mPage.getList();
                    mCategorySecondAdapter = new CategorySecondAdapter(mWares);
                    rvSecond.setAdapter(mCategorySecondAdapter);

                    if (totalPage==0){
                        int page = mPage.getTotalCount()/10;
                        if (mPage.getTotalCount()%10 != 0){
                            totalPage = page+1;
                        }else {
                            totalPage = page;
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<Page<Wares>> call, Throwable t) {

            }
        });
    }

    private void initFirstCategory() {
        rvFirst = view.findViewById(R.id.rvCategory_first);
        rvFirst.setLayoutManager(new LinearLayoutManager(getContext()));
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Contants.API.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        GetRequest_Interface service = retrofit.create(GetRequest_Interface.class);
        retrofit2.Call<List<Category>> call = service.categoryRequest();

        call.enqueue(new Callback<List<Category>>() {
            @Override
            public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {
                categoryFirstAdapter = new CategoryFirstAdapter(response.body());
                categoryFirstAdapter.setCategoryFirstListenner(new CategoryFirstAdapter.CategoryFirstListenner() {
                    @Override
                    public void onItemClick(long categoryId) {
                        curPage = 1;
                        state = NORMAL;
                        totalPage = 0;
                        mCategoryId = categoryId;
                        requestSecond();
                    }
                });
                rvFirst.setAdapter(categoryFirstAdapter);
            }

            @Override
            public void onFailure(Call<List<Category>> call, Throwable t) {

            }
        });
    }

    private void initBanners(){
        sliderShow = view.findViewById(R.id.slider_category);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Contants.API.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        GetRequest_Interface service = retrofit.create(GetRequest_Interface.class);
        retrofit2.Call<ArrayList<Banner>> call = service.bannerRequest();

        call.enqueue(new Callback<ArrayList<Banner>>() {
            @Override
            public void onResponse(Call<ArrayList<Banner>> call, Response<ArrayList<Banner>> response) {
                mBannersList = response.body();
                if (mBannersList!=null){
                    for (final Banner banner : mBannersList){
                        TextSliderView textSliderView = new TextSliderView(getContext());
                        textSliderView
                                .description(banner.getName())
                                .image(banner.getImgUrl());

                        textSliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                            @Override
                            public void onSliderClick(BaseSliderView slider) {
                                Toast.makeText(getContext(),banner.getName(),Toast.LENGTH_SHORT).show();
                            }
                        });
                        textSliderView.setScaleType(BaseSliderView.ScaleType.Fit);
                        sliderShow.addSlider(textSliderView);
                    }
                }
                sliderShow.setDuration(5000);
            }

            @Override
            public void onFailure(Call<ArrayList<Banner>> call, Throwable t) {

            }
        });
    }
}
